using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UMC.Data;
using UMC.Web;
using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow
{


    [Apiumc("WorkFlow", "KeyValue", Auth = WebAuthType.Admin, Desc = "字典配置")]
    class WorkFlowKeyValueActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var SId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g =>
            {
                var grid = new TaskKeyDialog() { Title = "字典配置", RefreshEvent = "WorkFlow.Key", IsSearch = true };
                grid.Menu("新建", request.Model, request.Command, "News");
                return grid;
            })) ?? 0;

            var taskForm =DataFactory.Instance().TaskKey(SId)?? new TaskKey();
            var Index = this.AsyncDialog("Index", g =>
            {
                if (taskForm.Id.HasValue == false)
                {
                    return this.DialogValue("$");
                }
                if (taskForm.Type == 1)
                {
                    return this.DialogValue("$");
                }
                else
                {
                    var ui = UISection.Create(new UITitle("字典类型"));

                    ui.AddCell("类型标识", taskForm.Code, new UIClick(request, g, "$"))
                    .AddCell("类型名称", taskForm.Caption)
                    .AddCell("子项类型", taskForm.Type == 1 ? "数据源" : "字典源");

                    ui.Title.Right(new UIEventText("新建码表").Click(new UIClick(request.Model, request.Command, new WebMeta(request.Arguments).Put(g, "@"))));
                    var uis = ui.NewSection();

                    Utility.Each(DataFactory.Instance().TaskKeyValue(SId).OrderBy(s => s.Seq ?? 0), dr =>
                        {
                            uis.AddCell(dr.Value, dr.Text, new UIClick(request.Model, request.Command, new WebMeta(request.Arguments).Put(g, dr.Value)));
                        });
                    if (uis.Length == 0)
                    {

                        uis.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未有码表配置,").Put("icon", "\uEA05").Put("N", "去新建"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),
                                           new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)).Name("N", new UIStyle().Click(new UIClick(request.Model, request.Command, new WebMeta(request.Arguments).Put(g, "@")))));
                    }
                    ui.SendTo(this.Context, "WorkFlow.Keys");
                }
                return this.DialogValue("none");
            });
            switch (Index)
            {
                case "$":
                    {
                        var vform = this.AsyncDialog("Settings", g =>
                        {

                            var form = new UIFormDialog() { Title = "字典类型" };
                            if (taskForm.Id.HasValue)
                            {
                                form.AddTextValue().Put("类型标识", taskForm.Code)
                                .Put("子项类型", taskForm.Type == 1 ? "数据源" : "字典源");

                                form.AddText("类型名称", "Caption", taskForm.Caption);
                                if (taskForm.Type == 1)
                                {
                                    form.AddText("值字段", "ValueKey", taskForm.ValueKey).Put("tip", "");
                                    form.AddOption("数据库", "ProviderKey", taskForm.ProviderKey, taskForm.ProviderKey).Command(this.Context.Request.Model, "Shape");

                                    form.AddTextarea("SQL脚本", "SelectSQL", taskForm.SelectSQL).Put("Rows", 6);//.NotRequired();
                                    form.AddFooter("在Select中 用as的字段将会显示（除值字段)");
                                }
                            }
                            else
                            {
                                form.Title = "新建字典类型";
                                form.AddText("类型标识", "Code", taskForm.Code);
                                form.AddText("类型名称", "Caption", taskForm.Caption);
                                form.AddRadio("子项类型", "Type")
                                .Put("字典源", "0", taskForm.Type == 0)
                                .Put("数据源", "1", taskForm.Type == 1);
                            }

                            form.Submit("确认", "WorkFlow.Key");

                            return form;
                        });
                        var tf = new TaskKey();
                        UMC.Data.Reflection.SetProperty(tf, vform.GetDictionary());
                        if (SId == 0)
                        {
                            if (DataFactory.Instance().TaskKey(tf.Code)!=null)
                            {
                                this.Prompt("类型标识已经存在");
                            }
                            tf.Id = Utility.NewId();
                            DataFactory.Instance().Put(tf);
                        }
                        else
                        {
                            DataFactory.Instance().Put(tf);
                        }
                    }
                    break;
                default:
                    {
                        var value = DataFactory.Instance().TaskKeyValue(SId, Index) ?? new TaskKeyValue();
                        var vform = this.AsyncDialog("Settings", g =>
                        {

                            var form = new UIFormDialog() { Title = "字典码表" };


                            form.AddText("字典代码", "Value", value.Value);
                            form.AddText("字典名称", "Text", value.Text);
                            form.AddNumber("展示顺序", "Seq", value.Seq);
                            if (value.KeyId.HasValue)
                                form.AddCheckBox("", "Disabled", "no").Put("删除", "true", value.Disabled == true);


                            form.Submit("确认", "WorkFlow.Keys");

                            return form;
                        });
                        var tf = new TaskKeyValue();
                        UMC.Data.Reflection.SetProperty(tf, vform.GetDictionary());
                        tf.Disabled = (vform["Disabled"] ?? "").Contains("true");
                        if (tf.Disabled == true)
                        {
                            DataFactory.Instance().Delete(new TaskKeyValue { KeyId = SId, Value = Index });

                        }
                        else
                        {

                            if (value.KeyId.HasValue && String.Equals(Index, "@") == false)
                            {
                                tf.KeyId = SId;
                                if (String.Equals(tf.Value, Index))
                                {

                                    DataFactory.Instance().Put(tf);
                                }
                                else
                                {
                                    if (DataFactory.Instance().TaskKeyValue(SId, tf.Value) != null)
                                    {
                                        this.Prompt("码值已存在");
                                    }
                                    else
                                    {
                                        DataFactory.Instance().Put(tf);

                                    }
                                }
                            }
                            else
                            {
                                if (DataFactory.Instance().TaskKeyValue(SId, tf.Value) != null)
                                {
                                    this.Prompt("码值已存在");
                                }
                                else
                                {
                                    tf.KeyId = SId;
                                    DataFactory.Instance().Put(tf);

                                }

                            }
                        }

                    }
                    this.Context.Send("WorkFlow.Keys", true);
                    break;
            }
            this.Context.Send("WorkFlow.Key", true);
        }

    }

}